var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
		when('/', {templateUrl: 'card.html', controller: paycont}).
		when('/cancel/', {templateUrl: 'card.html', controller: paycont}). 
		otherwise({redirectTo: '/'});
}]);

function paycont($scope, $http,$window,$location,$filter)
 {    
	$scope.currentPage = 0;
    $scope.pageSize = 25;
	$scope.Math = window.Math;	
	
	$scope.ed = false;	
	$scope.typearr = [];
		
	$http.get('carddata.php').success(function(data) {
		$scope.carddata = data;
		for(var i=0;i<data.length;i++)
			$scope.typearr[i] = data[i].num;
    });		
	
	$scope.save= function()
	{
		if(!$scope.num )
			alert("Please enter card number");
		else{
			var exist=false;			
			for(var i=0;i<$scope.typearr.length;i++)
				if($scope.num == $scope.typearr[i])
					exist=true;												
			if(exist)
				alert("This card number already exists");
			else{
				var sql = 'card_cud.php?q=c&dataset='+$scope.num;
				$http.get(sql).success(function(data) {						
					alert("Record saved");
				});
				$scope.cancel();
			}
		}			
	}
	
	$scope.edit= function(id)
	{
		for(i in $scope.carddata) {
			if($scope.carddata[i].id == id){          
				$scope.num = $scope.carddata[i].num;
				$scope.editid = id;
           }
        }
		$scope.ed = true;
	}
	
	$scope.upd= function()
	{
		if(!$scope.num )
			alert("Please enter card number");
		else{
			var exist=false;			
			for(var i=0;i<$scope.typearr.length;i++)
				if($scope.num == $scope.typearr[i])
					exist=true;												
			if(exist)
				alert("This card number already exists");
			else{
				var sql = 'card_cud.php?q=u&dataset='+$scope.num+'&id='+$scope.editid;
				$http.get(sql).success(function(data) {						
					alert("Record saved");
				});
				$scope.cancel();
			}
		}		
	}
	
	$scope.dlt= function(id)
	{		
		var sql = 'card_cud.php?q=d&id='+id;		
		$http.get(sql).success(function(data) {				
			alert("Record "+id+" deleted");
		});
		$scope.cancel();
	}
	
	$scope.cancel= function()
	{		 
		$scope.activePath = null;
		$scope.activePath = $location.path('card.php');	
	};
 }
 
 app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});

app.filter('startingFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});

 app.directive('myMaxlength', function() {
  return {
    require: 'ngModel',
    link: function (scope, element, attrs, ngModelCtrl) {
      var maxlength = Number(attrs.myMaxlength);
      function fromUser(text) {
          if (text.length > maxlength) {
            var transformedInput = text.substring(0, maxlength);
            ngModelCtrl.$setViewValue(transformedInput);
            ngModelCtrl.$render();
			alert(maxlength+" character limit reached...");
            return transformedInput;			
          } 
          return text;
      }
      ngModelCtrl.$parsers.push(fromUser);
    }
  }; 
});
